*********Procesamientos descriptivos*****************
set dp comma
sum subh age sex status_obj log_gdp gini p10 situ_econ [aw = wt]

tabmiss subh age sex status_obj log_gdp gini situ_econ p10 


*****************************modelos multinivel*********************************************
cd "C:\Users\xxxxxx\" // Configurar con la carpeta en la que se quieran guardar las salidas

*Centrado de variables
bysort countryyear:egen mediastatus_obj=mean(status_obj)
gen status_obj_cent = status_obj-mediastatus_obj

bysort countryyear:egen mediaage=mean(age)
gen age_cent = age-mediaage

gen age_cent2 = age_cent*age_cent

bysort countryyear:egen media_ideologia=mean(ideologia)
gen ideologia_cent = ideologia - media_ideologia

egen log_gpd_mean = mean(log_gdp)
gen log_gdp_cent = log_gdp - log_gpd_mean

egen gini_mean = mean(gini)
gen gini_cent = gini - gini_mean

egen p10_mean = mean(p10)
gen p10_cent = p10 - p10_mean

egen situ_econ_mean = mean(situ_econ)
gen situ_econ_cent = situ_econ - situ_econ_mean

drop mediaage mediaage log_gpd_mean gini_mean situ_econ_mean p10_mean

gen status_pib = status_obj_cent*log_gdp_cent
gen status_gini = status_obj_cent*gini_cent
gen status_econ = status_obj_cent*situ_econ_cent
gen status_p10 = status_obj_cent*p10_cent

*******Modelos REML*************
*Nulo
quietly mixed subh || country: || countryyear:, reml nonrtolerance
estimate store m1
quietly estat icc 
estadd scalar icc2 = r(icc2)
estadd scalar icc3 = r(icc3)

*Variables individuales
quietly mixed subh status_obj_cent i.sex age_cent age_cent2 || country: || countryyear:, reml nonrtolerance
estimate store m2

*Variables individuales y contextuales
quietly mixed subh status_obj_cent i.sex age_cent age_cent2 log_gdp_cent gini_cent p10_cent situ_econ_cent || country: || countryyear:, reml nonrtolerance
estimate store m3

*Variables individuales y contextuales + interaccion
quietly mixed subh status_obj_cent i.sex age_cent age_cent2 log_gdp_cent gini_cent p10_cent situ_econ_cent status_pib status_gini status_p10 status_econ||country: ||countryyear:, reml nonrtolerance
estimate store m4

*Variables individuales y contextuales + interaccion (pendiente aleatoria)
quietly mixed subh status_obj_cent i.sex age_cent age_cent2 log_gdp_cent gini_cent p10_cent situ_econ_cent status_pib status_gini status_p10 status_econ||country: ||countryyear: status_obj_cent, reml nonrtolerance
estimate store m5


esttab m1 m2 m3 m4 m5 using multinivel.rtf, cells(b(star fmt(3)) se(par fmt(2))) starlevels(* 0.10 ** 0.05 *** 0.01) transform(ln*: exp(2*@) 2*exp(2*@)) ///
legend stats(ll bic icc3 icc2 N, fmt(3 1 3 3 0) label(Log-likelihood  BIC ICC(País) "ICC(país y año)" N)) nonumbers mtitles ("M1" "M2" "M3" "M4" "M5") ///
addnote ("Fuente: elaboración propia en base a Latinobarometro 2006-2013") replace


************Pruebas de modelos M4 y M5***************
quietly mixed subh status_obj_cent i.sex age_cent age_cent2 log_gdp_cent gini_cent p10_cent situ_econ_cent status_pib status_gini status_p10 status_econ||country: ||countryyear:, ml 
est store m4

quietly mixed subh status_obj_cent i.sex age_cent age_cent2 log_gdp_cent gini_cent p10_cent situ_econ_cent status_pib status_gini status_p10 status_econ||country: ||countryyear: status_obj_cent, ml 
est store m5

lrtest m4 m5


